package com.br.android.widing;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widing.adapter.ListCategoriaAdapter;
import com.br.android.widing.db.WidingCategoriaCursor;
import com.br.android.widing.db.WidingCategoriaDAO;
import com.br.android.widing.entity.Categoria;

public class ListaCategoriasActivity extends SherlockActivity {

	private ListView listViewCategorias;
	private WidingCategoriaDAO dao;
	private WidingCategoriaCursor cursor;
	
	private List<Categoria> listCategorias;
	
	ActionMode actionMode;
	
	private ListCategoriaAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_list_categorias);
		
		listViewCategorias = (ListView) findViewById(R.id.listViewCategorias);
		
		buscarNoBanco();
		
		listViewCategorias.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
				Categoria c = listCategorias.get(position);
				actionMode = startActionMode(new ActionModeMenus(c));
			}
		});
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	public void buscarNoBanco(){
		listCategorias = new ArrayList<Categoria>();
		dao = new WidingCategoriaDAO(this);
		cursor = dao.consultarCategorias();
		for(int i = 0; i < cursor.getCount(); i++){
			cursor.moveToPosition(i);
			Categoria c = new Categoria();
			c.setIdcategoria(cursor.getIdCategoria());
			c.setNomecategoria(cursor.getNomeCategoria());
			listCategorias.add(c);
		}
		adapter = new ListCategoriaAdapter(this, listCategorias);
		listViewCategorias.setAdapter(adapter);
		cursor.close();
		dao.close();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_principal, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_novo_gasto:
			startActivity(new Intent(this, AdicionarCategoriaActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		case android.R.id.home:
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {
		startActivity(new Intent(this, PrincipalActivity.class));
		overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		finish();
	}
	
	public final class ActionModeMenus implements Callback {

		Categoria categoria;
		
		public ActionModeMenus(Categoria c){
			this.categoria = c;
		}
		
		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			menu.add("Editar")
            .setIcon(R.drawable.icon_edit)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
	       
			menu.add("Excluir")
            .setIcon(R.drawable.icon_discard)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
			
			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			if(item.getTitle().toString().equals("Editar")){
				View view = getLayoutInflater().inflate(R.layout.layout_custom_dialog, null);
				final EditText editText = (EditText) view.findViewById(R.id.editTextEditarCategoria);
				editText.setText(categoria.getNomecategoria());
				AlertDialog.Builder alert = new AlertDialog.Builder(ListaCategoriasActivity.this);
				alert.setTitle("Editar Categoria");
				alert.setMessage("Digite um novo nome para a categoria:");
				alert.setPositiveButton("Alterar", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						categoria.setNomecategoria(editText.getText().toString());
						dao.alterarCategoria(categoria);
						buscarNoBanco();
						dialog.dismiss();
						actionMode.finish();
					}
				});
				alert.setNegativeButton("Cancelar", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						actionMode.finish();
					}
				});
				AlertDialog ad = alert.create();
				ad.setView(view);
				ad.show();
			}
			if(item.getTitle().toString().equals("Excluir")){
				AlertDialog.Builder alert = new AlertDialog.Builder(ListaCategoriasActivity.this);
				alert.setTitle(categoria.getNomecategoria());
				alert.setMessage("Tem certeza que deseja excluir esta categoria?");
				alert.setPositiveButton("Excluir", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						listCategorias.remove(categoria);
						dao.excluirCategoria(categoria.getIdcategoria());
						adapter.notifyDataSetChanged();
						dialog.dismiss();
						actionMode.finish();
					}
				});
				alert.setNegativeButton("Cancelar", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				alert.show();
			}
			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
}
